1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Data.SqlClient;
10 namespace WarehouseManagementSystem
11 {
12 public partial class frmSubCategoryRecord : Form
13 {
14 SqlDataReader rdr = null;
15 SqlConnection con = null;
16 SqlCommand cmd = null;
17 ConnectionString cs = new ConnectionString();
18 public frmSubCategoryRecord()
19 {
20 InitializeComponent();
21 }
22
23 private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
24 {
25 string strRowNumber = (e.RowIndex + 1).ToString();
26 SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
27 if (dataGridView1.RowHeadersWidth < Convert.ToInt32((size.Width + 20)))
28 {
29 dataGridView1.RowHeadersWidth = Convert.ToInt32((size.Width + 20));
30 }
31 Brush b = SystemBrushes.ControlText;
32 e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
33 }
34
35 private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
36 {
37 DataGridViewRow dr = dataGridView1.SelectedRows[0];
38 this.Hide();
39 frmSubCategory frm = new frmSubCategory();
40 // or simply use column name instead of index
41 //dr.Cells["id"].Value.ToString();
42 frm.Show();
43 frm.txtSubCategoryID.Text = dr.Cells[0].Value.ToString();
44 frm.txtSubCategory.Text = dr.Cells[1].Value.ToString();
45 frm.txtCategoryID.Text = dr.Cells[2].Value.ToString();
46 frm.cmbCategory.Text = dr.Cells[3].Value.ToString();
47 frm.btnDelete.Enabled = true;
48 frm.btnUpdate.Enabled = true;
49 frm.txtSubCategory.Focus();
50 frm.btnSave.Enabled = false;
51 }
52
53 private void frmCompanyRecord_Load(object sender, EventArgs e)
54 {
55 GetData();
56 }
57 public void GetData()
58 {
59 try
60 {
61 con = new SqlConnection(cs.DBConn);
62 con.Open();
63 String sql = "SELECT RTRIM(SubCategory.ID),RTRIM(SubCategoryName),RTRIM(CategoryID),RTRIM(CategoryName) from Category,SubCategory where Category.ID=SubCategory.CategoryID order by SubCategoryName";
64 cmd = new SqlCommand(sql, con);
65 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
66 dataGridView1.Rows.Clear();
67 while (rdr.Read() == true)
68 {
69 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3]);
70 }
71 con.Close();
72 }
73 catch (Exception ex)
74 {
75 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
76 }
77 }
78
79 private void frmCompanyRecord_FormClosing(object sender, FormClosingEventArgs e)
80 {
81 this.Hide();
82 frmSubCategory frm = new frmSubCategory();
83 frm.Show();
84 }
85
86 private void txtSubCategory_TextChanged(object sender, EventArgs e)
87 {
88 try
89 {
90 con = new SqlConnection(cs.DBConn);
91 con.Open();
92 String sql = "SELECT RTRIM(SubCategory.ID),RTRIM(SubCategoryName),RTRIM(CategoryID),RTRIM(CategoryName) from Category,SubCategory where Category.ID=SubCategory.CategoryID and SubCategoryName like '" + txtSubCategory.Text + "%' order by SubCategoryName";
93 cmd = new SqlCommand(sql, con);
94 rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
95 dataGridView1.Rows.Clear();
96 while (rdr.Read() == true)
97 {
98 dataGridView1.Rows.Add(rdr[0], rdr[1], rdr[2], rdr[3]);
99 }
100 con.Close();
101 }
102 catch (Exception ex)
103 {
104 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
105 }
106 }
107 }
108 }